package com.leyou.item.service;


import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpecService {


    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    /**
     * 查询规格
     * @param id
     * @return
     */
    public List<SpecGroupDTO> queryGroupByCategory(Long id) {

        SpecGroup specGroup = new SpecGroup();

        specGroup.setCid(id);

        List<SpecGroup> list = specGroupMapper.select(specGroup);

        if (CollectionUtils.isEmpty(list)){

            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(list, SpecGroupDTO.class);


    }




    /**
     * 查询Param
     * @param gid
     * @param cid
     * @param searching
     * @return
     */
    public List<SpecParamDTO> querySpecParams(Long gid, Long cid, Boolean searching) {

        SpecParam specParam = new SpecParam();

        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);

        List<SpecParam> list = specParamMapper.select(specParam);

        if (CollectionUtils.isEmpty(list)){

            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(list,SpecParamDTO.class);
    }


    /**
     *  添加规格
     * @param specGroupDTO
     */
    public void saveGroupByCategory(SpecGroupDTO specGroupDTO) {

        SpecGroup specGroup = BeanHelper.copyProperties(specGroupDTO, SpecGroup.class);


        int count = specGroupMapper.insertSelective(specGroup);

        if (count != 1 ){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }

    }


    /**
     * 删除规格
     * @param id
     */
    public void deleteGroupByCategory(Long id) {

        int count = specGroupMapper.deleteByPrimaryKey(id);
        if (count != 1){
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }


    /**
     * 修改规格
     * @param specGroupDTO
     */
    public void updateGroupByCategory(SpecGroupDTO specGroupDTO) {

        int count = specGroupMapper.updateByPrimaryKeySelective(BeanHelper.copyProperties(specGroupDTO, SpecGroup.class));

        if (count != 1){
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }

    }

    /**
     *  添加Param
     * @param specParamDTO
     */
    public void saveParam(SpecParamDTO specParamDTO) {

        SpecParam specParam = BeanHelper.copyProperties(specParamDTO, SpecParam.class);
        int count = specParamMapper.insertSelective(specParam);
        if (count != 1){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 修改Param
     * @param specParamDTO
     */
    public void updateParam(SpecParamDTO specParamDTO) {

        int count = specParamMapper.updateByPrimaryKeySelective(BeanHelper.copyProperties(specParamDTO, SpecParam.class));
        if (count != 1){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 删除Param
     * @param id
     */
    public void deleteParam(Long id) {

        int count = specParamMapper.deleteByPrimaryKey(id);
        if (count != 1){
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }

    public List<SpecGroupDTO> querySpecsByCid(Long id) {
        //查询规格组
        List<SpecGroupDTO> list = queryGroupByCategory(id);

        //查询规格参数
        List<SpecParamDTO> specParams = querySpecParams(null, id, null);
        //把参数分组，key是groupid，值是group下的所有Param的集合
        Map<Long, List<SpecParamDTO>> map = specParams.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        //把分好的组set到Froup里
        /*for (SpecGroupDTO specGroupDTO : list) {
            specGroupDTO.setParams(map.get(specGroupDTO.getId()));
        }*/
        list.forEach(specGroupDTO -> specGroupDTO.setParams(map.get(specGroupDTO.getId())));



        /*//把参数分组，key是groupid，值是group下的所有Param的集合
        Map<Long,List<SpecParamDTO>> map = new HashMap<>();
        for (SpecParamDTO specParam : specParams) {
            //判断当前这个param所属的group是否已经在map中存在了
            if (!map.containsKey(specParam.getGroupId())){
                //不存在，说明是当前group的第一个参数，那么我们新增一个List进去
                map.put(specParam.getGroupId(),new ArrayList<>());
            }
            // 存在，则取出group下的List，然后把param加进去
            List<SpecParamDTO> specParamDTOS = map.get(specParam.getGroupId());
            specParamDTOS.add(specParam);
        }

        //把分好的组set到Froup里
        for (SpecGroupDTO specGroupDTO : list) {
            specGroupDTO.setParams(map.get(specGroupDTO.getId()));
        }*/





       /* //查询规格参数
        for (SpecGroupDTO group : list) {
            Long groupId = group.getId();
            //根据组id查询组内参数
            SpecParam specParam = new SpecParam();
            specParam.setGroupId(groupId);
            List<SpecParam> params = specParamMapper.select(specParam);
            if (CollectionUtils.isEmpty(params)){
                throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
            }
            List<SpecParamDTO> paramDTOS = BeanHelper.copyWithCollection(params, SpecParamDTO.class);
            group.setParams(paramDTOS);
        }*/


        return list;
    }
}
